package com.cxf.windex.base.rest;

import com.cxf.windex.base.biz.BaseBiz;
import com.cxf.windex.base.common.DataAccessException;
import com.cxf.windex.base.common.ServiceException;
import com.cxf.windex.base.constant.UserConstant;
import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.secruity.JwtTokenUtil;
import com.cxf.windex.base.secruity.JwtUser;
import com.cxf.windex.base.util.CommonUtil;
import com.cxf.windex.base.util.MapCache;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author wanghaobin
 * @create 2017-06-15 8:48
 */
public class BaseRestController<Biz extends BaseBiz,Entity> {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected Biz baseBiz;
    /**
     * 文件存放 路径
     */
    @Value("${upload.path}")
    private String uploadPath;

    /**
     * 文件访问路径
     */
    @Value("${upload.url}")
    private String uploadUrl;

    protected MapCache cache = MapCache.single();
    @Autowired
    protected CacheManager cacheManager;

    protected Map<String, Object> resultMap = new HashMap<String, Object>();


    protected String getClientIp(){
        return CommonUtil.getClientIp(request);
    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    @ResponseBody
    public ObjectRestResponse<Entity> add(Entity entity){
        baseBiz.insertSelective(entity);
        return new ObjectRestResponse<Entity>().rel(true);
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public ObjectRestResponse<Entity> update(Entity entity){
        baseBiz.updateSelectiveById(entity);
        return new ObjectRestResponse<Entity>().rel(true);
    }
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public ObjectRestResponse<Entity> remove(@PathVariable Long id){
        baseBiz.deleteById(id);
        return new ObjectRestResponse<Entity>().rel(true);
    }

    public String getCurrentUserType() {
        SecurityContext sc = SecurityContextHolder.getContext();
        Object principal = sc.getAuthentication().getDetails();
        if (principal instanceof JwtUser) {
            JwtUser user = (JwtUser) principal;
            return user.getTypeId();
        }
        return "0";
    }



    public String getCurrentUserId(){
        SecurityContext sc = SecurityContextHolder.getContext();
        if(sc.getAuthentication()==null){
            throw new ServiceException("用户未登陆");
        }

        Object principal = sc.getAuthentication().getDetails();
        if (principal instanceof JwtUser) {
            JwtUser user = (JwtUser) principal;
            return user.getId();
        }
        throw new ServiceException("用户未登陆");
    }



    /**
     * 返回结果信息
     *
     * @param code 0成功 -1失败
     * @param message 结算信息
     */
    public void resultMap(String code, String message) {

        resultMap.put("code", code);
        resultMap.put("msg", message);

    }
}
